package com.example.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.crm.entity.Customer;
import com.example.crm.service.CustomerService;
import com.example.crm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 客户API控制器
 */
@RestController
@RequestMapping("/api/customer")
public class CustomerAPIController {

    @Autowired
    private CustomerService customerService;

    /**
     * 分页查询客户
     */
    @GetMapping("/list")
    public Result<IPage<Customer>> list(@RequestParam(required = false) Map<String, Object> params) {
        if (params == null) {
            params = new HashMap<>();
        }
        IPage<Customer> pageData = customerService.listCustomersByPage(params);
        return Result.success(pageData);
    }

    /**
     * 新增客户
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody Customer customer) {
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customer.setDeleted(0);
        return Result.success(customerService.addCustomer(customer, null));
    }

    /**
     * 修改客户
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody Customer customer) {
        customer.setUpdateTime(LocalDateTime.now());
        return Result.success(customerService.updateCustomer(customer, null));
    }

    /**
     * 删除客户
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(customerService.removeById(id));
    }

    /**
     * 获取客户详情
     */
    @GetMapping("/{id}")
    public Result<Customer> getById(@PathVariable Long id) {
        return Result.success(customerService.getCustomerDetail(id));
    }
    
    /**
     * 获取客户统计信息
     */
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getStatistics() {
        return Result.success(customerService.getCustomerStatistics());
    }
} 